//
//  YTRegisterViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/3.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTRegisterViewController.h"
#import "YTCountryCodeViewController.h"
#import "YTRegisterViewModel.h"
#import "YTWebViewController.h"

@interface YTRegisterViewController ()<YTCountryCodeDelegate>

@property (weak, nonatomic) IBOutlet UIButton *areaCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *verifyCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *checkProtocolBtn;
@property (weak, nonatomic) IBOutlet UIButton *eyeButton;

@property (weak, nonatomic) IBOutlet UITextField *phoneNum;
@property (weak, nonatomic) IBOutlet UITextField *verifyNum;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *inviteCode;

@property (strong, nonatomic) YTRegisterViewModel *viewModel;

@end

@implementation YTRegisterViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self yt_changStatusBarWithStyle:UIStatusBarStyleLightContent];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

#pragma mark - super

- (void)bindViewModel {
    RAC(_viewModel, phoneNum) = _phoneNum.rac_textSignal;
    RAC(_viewModel, password) = _password.rac_textSignal;
    RAC(_viewModel, verifyCode) = _verifyNum.rac_textSignal;
    RAC(_viewModel, inviteCode) = _inviteCode.rac_textSignal;
    RAC(_viewModel, isChecked) = RACObserve(_checkProtocolBtn, selected);
    RAC(_viewModel, areaCode) = RACObserve(_areaCodeBtn.titleLabel, text);
    RAC(_viewModel, isChina) = RACObserve(self, isChina);
    _verifyCodeBtn.rac_command = _viewModel.sendVerifyCommand;
    _registerBtn.rac_command = _viewModel.registerCommand;
    
    UIColor *(^validateColor)(NSNumber *) = [NSObject yt_btnNormalColor:[UIColor blackColor] disabled:[UIColor redColor]];
    RAC(_phoneNum, textColor) = [[_viewModel.phoneValidate distinctUntilChanged] map:validateColor];
    RAC(_password, textColor) = [[_viewModel.passwordValidate distinctUntilChanged] map:validateColor];
    RAC(_verifyNum, textColor) = [[_viewModel.verifyValidate distinctUntilChanged] map:validateColor];
    
    UIColor *(^btnColor)(NSNumber *) = [NSObject yt_btnNormalColor:kMainBlueColor disabled:[UIColor lightGrayColor]];
    RAC(_registerBtn, backgroundColor) = [[_viewModel.allValidate distinctUntilChanged] map:btnColor];
    RAC(_verifyCodeBtn, backgroundColor) = [[_viewModel.phoneValidate distinctUntilChanged] map:btnColor];
    
    @weakify(self);
    [[_viewModel.sendVerifyCommand.executionSignals switchToLatest] 
     subscribeNext:^(NSString *title) {
         @strongify(self);
         [self.verifyCodeBtn setTitle:title forState:UIControlStateNormal];
    }];
    
    [[_viewModel.registerCommand.executionSignals switchToLatest] subscribeNext:^(NSDictionary *response) {
        @strongify(self);
        if ([response[@"resultCode"] integerValue] == 1000) {
            RACTuple *userInfo = RACTuplePack(self.phoneNum.text, self.password.text);
            [[NSNotificationCenter defaultCenter] postNotificationName:YTUserRegisterSuccessNotification object:userInfo];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
}

- (void)initialize {
    _viewModel = [[YTRegisterViewModel alloc] init];
    @weakify(self);
    [[self.eyeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *btn) {
        @strongify(self);
        btn.selected = !btn.selected;
        self.password.secureTextEntry = !btn.selected;
    }];
    
    [[self.checkProtocolBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *btn) {
        btn.selected = !btn.selected;
    }];
    
    [[_areaCodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        YTCountryCodeViewController *countryCode = [[YTCountryCodeViewController alloc] init];
        countryCode.delegate = self;
        [self.navigationController pushViewController:countryCode animated:YES];
    }];
}

- (IBAction)popRootAction:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)agreementAction:(UIButton *)sender {
    YTWebViewController *web = [[YTWebViewController alloc] init];
    web.urlString = @"http://api.yitu8.cn/api/driver/v2.0/h5/agreement.htm";
    [self.navigationController pushViewController:web animated:YES];
}

- (void)didSelectWithCountryCode:(NSString *)countryCode {
    [_areaCodeBtn setTitle:[NSString stringWithFormat:@"+ %@", countryCode] forState:UIControlStateNormal];
}

@end
